/**
 * 管理界面
 */
Ext.define('cc.noself.view.MngNoSelfBus', {
	extend: 'Ext.container.Container',
	uses: ['cc.util.NoselfService',
	       'cc.noself.view.NoSelfBusGridView',
	       'cc.noself.view.NoSelfBus',
	       'bmc.common.comp.EntityWindow'],
	layout: {
		type: 'hbox',
		align: 'stretch'
	},
	
	viewModel: {
		data: {
			auth: {
				create: true,
				update: true,
				'delete': true
			},
			noSelfBus: null
		}
	},
	
	initComponent: function(){
		var me = this;
		
		me.noSelfBusGridView = Ext.create('cc.noself.view.NoSelfBusGridView', {
			flex: 1,
			listeners: {
				//切换viewModel
				selectionchange: function(scope, recs){
					me.getViewModel().set('noSelfBus', recs[0]);
				}
			},
			tbar: [{
				text: '新增',
				iconCls: 'c-btn-create',
				bind: {
					disabled: '{!auth.create}'
				},
				handler: function(){
					var r = Ext.create('cc.noself.model.NoSelfBus', {
						id: 0
					});

					me.showNoSelfBusWin('新增', r, true);
				}
			}, {
				text: '编辑',
				iconCls: 'c-btn-update',
				bind: {
					disabled: '{!auth.update}'
				},
				handler: function(){
					var r = me.getViewModel().get('noSelfBus');
					if(r){
						me.showNoSelfBusWin('编辑', r, false);
					}
				}
			}, {
				text: '删除',
				iconCls: 'c-btn-delete',
				bind: {
					disabled: '{!auth.delete}'
				},
				handler: function(){
					var r = me.getViewModel().get('noSelfBus');
					if(r){
						Ext.Msg.confirm('提示', '即将删除该记录，是否继续？', function(btn){
							if(btn == 'yes'){
								CC.Noself.deleteNoSelfBus(r.get('id'), function(){
									me.noSelfBusGridView.store.remove(r);
								});
							}
						});
					}
				}
			}]
		});
		me.items = [me.noSelfBusGridView];
		
		me.callParent(arguments);
	},
	
	//显示实体窗口
	showNoSelfBusWin: function(title, rec, add){
		var me = this;
		var form = Ext.create('cc.noself.view.NoSelfBus', {
			viewModel: {
				data: {
					rec: rec
				}
			}
		});
//		var form;
//		if(add){
//			form = Ext.create('cc.noself.view.NoSelfBus', {
//				viewModel: {
//					data: {
//						rec: rec
//					}
//				}
//			});
//		}else{
//			form = Ext.create('cc.noself.view.NoSelfBus');
//			form.loadEntity(rec.get('id'));
//		}
		
		var win = Ext.create('bmc.common.comp.EntityWindow', {
			width: 400,
			title: title,
			form: form,
			cb: function(){
				if(add){
					me.noSelfBusGridView.store.insert(0, rec);
				}
			},
			listeners: {
				reject: function(){
					rec.reject();
				}
			}
		});
		win.show();
	}
});
